package com.kalix.training.live.biz;

import com.kalix.framework.core.api.persistence.JsonData;
import com.kalix.framework.core.api.persistence.JsonStatus;
import com.kalix.framework.core.impl.biz.ShiroGenericBizServiceImpl;
import com.kalix.training.live.api.biz.ILiveRecordBeanService;
import com.kalix.training.live.api.dao.ILiveRecordBeanDao;
import com.kalix.training.live.entities.LiveRecordBean;

import java.util.List;

public class LiveRecordBeanServiceImpl extends ShiroGenericBizServiceImpl<ILiveRecordBeanDao, LiveRecordBean>
        implements ILiveRecordBeanService {

    @Override
    public void beforeSaveEntity(LiveRecordBean entity, JsonStatus status) {
        entity.setSign("是");
    }


    /**
     * @Author lwj
     * @Description 关闭直播
     * @Date 9:24 2019/3/15
     **/
    public String updateSign(Long id){
        int s = dao.updateNativeQuery("update \"public\".\"mooc_live_record\" set sign=\"否\" WHERE liveid="+id);
        System.out.println(s);
        return String.valueOf(s);
    }


    /**
     * @Author lwj
     * @Description 查询直播人数
     * @Date 9:37 2019/3/15
     **/
    public JsonData findLivePerson(Long liveId){
        List<LiveRecordBean> liveRecordBeanList= dao.findArrayByNativeSql("SELECT * FROM \"public\".\"mooc_live_record\" where liveid="+liveId+" AND sign='是'", LiveRecordBean.class);
        JsonData jsonData = new JsonData();
        jsonData.setTotalCount((long)liveRecordBeanList.size());
        jsonData.setData(liveRecordBeanList);
        return jsonData;
    }

    /**
     * @Author lwj
     * @Description 增加直播间人数
     * @Date 9:37 2019/3/15
     **/
    public JsonData addNumber(Long liveId){
        List<LiveRecordBean> liveRecordBeanList= dao.findArrayByNativeSql("SELECT * FROM \"public\".\"mooc_live_record\" where liveid="+liveId+" AND sign='是'", LiveRecordBean.class);
        Long num = liveRecordBeanList.get(0).getNumber();
        num +=1;
        dao.updateNativeQuery("UPDATE \"public\".\"mooc_live_record\" SET number = " + num + "  WHERE liveid = " + liveId);
        return findLivePerson(liveId);
    }

    /**
     * @Author lwj
     * @Description 减少直播间人数
     * @Date 9:37 2019/3/15
     **/
    public JsonData reduceNumber(Long liveId){
        List<LiveRecordBean> liveRecordBeanList= dao.findArrayByNativeSql("SELECT * FROM \"public\".\"mooc_live_record\" where liveid="+liveId+" AND sign='是'", LiveRecordBean.class);
        Long num = liveRecordBeanList.get(0).getNumber();
        num -=1;
        dao.updateNativeQuery("UPDATE \"public\".\"mooc_live_record\" SET number = " + num + "  WHERE liveid = " + liveId);
        return findLivePerson(liveId);
    }

}
